package com.flycent.fanbao.ui;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.flycent.fanbao.App;
import com.flycent.fanbao.Constants;
import com.flycent.fanbao.R;

/**
 * @author fbl
 *  翻译
 */
public class TranslationActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.translation_text1) EditText mEditText1;
	@ViewInject(id = R.id.translation_text2) TextView mEditText2;
	@ViewInject(id = R.id.translation_but, click = "onClick") Button mBut;

	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_translation);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("翻译");
	}

	public void onClick(View v){
		if( v == mBut ){
			if( invalidateText(mEditText1)){
				App.showShortToast("请输入翻译的内容");
				return;
			}
			doTranslation(getText(mEditText1));
		}
	}


	private void doTranslation(String str) {
		showLoadingDialog("");

		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("client_id", "dIlef3U9breRus6Qt4DtkD6A");
		params.put("from", "auto");
		params.put("to", "auto");
		params.put("q", str);
		fh.get(Constants.TranslationIP,params, new AjaxCallBack<Object>() {

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				super.onFailure(t, errorNo, strMsg);
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object t) {
				super.onSuccess(t);
				dismissLoadingDialog();
				try {
					JSONObject jsonObject  = new JSONObject(t.toString());

					String ss = jsonObject.getJSONArray("trans_result").getJSONObject(0).getString("dst").toString();

					App.showLog("ss=="+ss);
					mEditText2.setText(ss);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

}
